//
//  YRStatusCell.swift
//  新浪微博
//
//  Created by Viper on 16/6/28.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
import SnapKit
let StatusMargin : CGFloat = 10
class YRStatusCell: UITableViewCell {
    //记录底部约束
    var toolbarTopConstraint:Constraint?
    //供外界给cell设置数据
    var status : YRStauts?{
        //外界给其赋值的时候会走didSet方法
        didSet{
            
            // 给原创微博赋值
            //没有转发微博
            statusOrigianlView.status = status
            
            //卸载底部toolbar的top原有约束
           
            toolbarTopConstraint?.uninstall()
            
            if status?.retweeted_status == nil {
                
              statusToolbar.snp_updateConstraints(closure: { (make) in
                
              toolbarTopConstraint = make.top.equalTo(statusOrigianlView.snp_bottom).constraint
              })
                
                StatusRetweetView.hidden = true
                
                }else{
                //有转发微博
                //先赋值 后更新约束
                StatusRetweetView.status = status
                
                StatusRetweetView.hidden = false
                //如果已经卸载该约束  重新设置约束使用make 和 update一样
                statusToolbar.snp_updateConstraints(closure: { (make) in
                    
                  toolbarTopConstraint = make.top.equalTo(StatusRetweetView.snp_bottom).constraint
                })
            }
            //给toolbar赋值
            statusToolbar.status = status
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        
    }
   required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        contentView.addSubview(statusOrigianlView)
        contentView.addSubview(statusToolbar)
        //转发微博
        contentView.addSubview(StatusRetweetView)
        
        //添加约束
        statusOrigianlView.snp_makeConstraints { (make) in
            
            make.leading.trailing.equalTo(contentView)
            //高度已经在原创微博动态计算
//            make.height.equalTo(50)
            make.top.equalTo(contentView).offset(8)
            
        }
        
        statusToolbar.snp_makeConstraints { (make) in
            
            make.leading.trailing.equalTo(contentView)
            //记录约束
            toolbarTopConstraint = make.top.equalTo(StatusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolbar)
        }
        
        
        StatusRetweetView.snp_makeConstraints { (make) in
            
            make.top.equalTo(statusOrigianlView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
//            make.height.equalTo(50)
        }
        
        

        
    }
    
    //MARK: - 懒加载控件
    private lazy var statusOrigianlView : YRStatusOriginalView = YRStatusOriginalView()
   
    //底部bar
    private lazy var statusToolbar : YRStatusTabBar = YRStatusTabBar()
    
    //转发微博
    private lazy var StatusRetweetView : YRStatusRetweetView = YRStatusRetweetView()
    
}
